package com.xprime.mobileFramework.function;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREInvalidObjectException;
import com.adobe.fre.FREObject;
import com.adobe.fre.FRETypeMismatchException;
import com.adobe.fre.FREWrongThreadException;

public class AlertDialogFREFunction implements FREFunction {
	
	

	@Override
	public FREObject call(FREContext pContext, FREObject[] pParams) {
		Activity activity = pContext.getActivity();
		final FREContext freContext = pContext;
		try {
			
			String title = pParams[0].getAsString();
			String message = pParams[1].getAsString();			

			Builder alertBuilder = new Builder(activity);
			alertBuilder.setTitle(title);
			alertBuilder.setMessage(message);
			alertBuilder.setNegativeButton("Cancel", new OnClickListener() {				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					freContext.dispatchStatusEventAsync("alertDialog", "cancel");					
				}
			}).setNeutralButton("OK", new OnClickListener() {				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					freContext.dispatchStatusEventAsync("alertDialog", "ok");
				}
			});
			alertBuilder.create().show();

			return FREObject.newObject(true);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (FRETypeMismatchException e) {
			e.printStackTrace();
		} catch (FREInvalidObjectException e) {
			e.printStackTrace();
		} catch (FREWrongThreadException e) {
			e.printStackTrace();
		}
		
		return null;
	}

}
